//
//  BLSupplyViewController.m
//  blanket
//
//  Created by Mac on 2017/12/4.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "BLSupplyViewController.h"
#import "BLClothMangeViewController.h"
#import "BLRepertoryViewController.h"
#import "BLClosthWashViewController.h"
#import "BLClothCustomViewController.h"
#import "BLShopingViewController.h"
#import "BLWathClosthHomeViewController.h"
@interface BLSupplyViewController ()
@property(nonatomic, strong) BLUserModel                     *userModel;
@end

@implementation BLSupplyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     self.title = @"补给站";
    [self configSubView];
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self sendMyInfoRequest];
}
#pragma mark --subView
- (void)configSubView{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 44)];
    header.backgroundColor = [UIColor whiteColor];
    
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 156)];
    imageView.image = [UIImage imageNamed:@"abnner2"];
    [header addSubview:imageView];
    
    UIView * caijiView = [self headerViewWithTitle:@"酒店采集"];
    caijiView.top = imageView.bottom;
    [header addSubview:caijiView];
    
    //底部
    STMenuControl * xiaohaoControl = [[STMenuControl alloc] initWithFrame:CGRectMake(0, caijiView.bottom, UIScreenWidth / 4, UIScreenWidth / 4) imageName:@"消耗品"
                                                                    title:@"消耗品" titleColor:BL_secendTextColor];
    [xiaohaoControl addTarget:self action:@selector(onSelectedControl:) forControlEvents:UIControlEventTouchUpInside];
    [header addSubview:xiaohaoControl];
    
    STMenuControl * dingzhiControl = [[STMenuControl alloc] initWithFrame:CGRectMake(0, caijiView.bottom, UIScreenWidth / 4, UIScreenWidth / 4) imageName:@"量身定制"
                                                                    title:@"布草定制" titleColor:BL_secendTextColor];
    [dingzhiControl addTarget:self action:@selector(onSelectedControl:) forControlEvents:UIControlEventTouchUpInside];
    dingzhiControl.left = UIScreenWidth /4;
    [header addSubview:dingzhiControl];
    
    UIView * grawView = [[UIView alloc] initWithFrame:CGRectMake(0,xiaohaoControl.bottom, UIScreenWidth, 20)];
    grawView.backgroundColor = self.tableView.backgroundColor;
    [header addSubview:grawView];
    
    UIView * xidiView = [self headerViewWithTitle:@"洗涤服务"];
    xidiView.top = grawView.bottom;
    [header addSubview:xidiView];
    STMenuControl * zengjiaControl = [[STMenuControl alloc] initWithFrame:CGRectMake(0, xidiView.bottom, UIScreenWidth / 4, UIScreenWidth / 4) imageName:@"增减"
                                                                    title:@"布草增减" titleColor:BL_secendTextColor];
    [zengjiaControl addTarget:self action:@selector(onSelectedControl:) forControlEvents:UIControlEventTouchUpInside];
    zengjiaControl.left = 0;
    [header addSubview:zengjiaControl];
    
    STMenuControl * xidiControl = [[STMenuControl alloc] initWithFrame:CGRectMake(0, xidiView.bottom, UIScreenWidth / 4, UIScreenWidth / 4) imageName:@"洗涤"
                                                                    title:@"布草洗涤" titleColor:BL_secendTextColor];
    [xidiControl addTarget:self action:@selector(onSelectedControl:) forControlEvents:UIControlEventTouchUpInside];
    xidiControl.left = UIScreenWidth / 4;;
    [header addSubview:xidiControl];
    
    STMenuControl * chonghziControl = [[STMenuControl alloc] initWithFrame:CGRectMake(0, xidiView.bottom, UIScreenWidth / 4, UIScreenWidth / 4) imageName:@"充值2"
                                                                 title:@"洗涤充值" titleColor:BL_secendTextColor];
    [chonghziControl addTarget:self action:@selector(onSelectedControl:) forControlEvents:UIControlEventTouchUpInside];
    chonghziControl.left = 2 * UIScreenWidth / 4;;
    [header addSubview:chonghziControl];
    
    STMenuControl * kucunControl = [[STMenuControl alloc] initWithFrame:CGRectMake(0, xidiView.bottom, UIScreenWidth / 4, UIScreenWidth / 4) imageName:@"查看库存"
                                                                     title:@"查看库存" titleColor:BL_secendTextColor];
    [kucunControl addTarget:self action:@selector(onSelectedControl:) forControlEvents:UIControlEventTouchUpInside];
    kucunControl.left = 3 * UIScreenWidth / 4;;
    [header addSubview:kucunControl];
    
    header.height = kucunControl.bottom + 5;
    self.tableView.tableHeaderView = header;
}
- (UIView*)headerViewWithTitle:(NSString*)title{
    UIView * sectionHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 44)];
    sectionHeader.backgroundColor = [UIColor whiteColor];
    STButton * line = [[STButton alloc] initWithFrame:CGRectMake(15, 0, 4, 22)
                                                title:nil
                                           titleColor:nil
                                            titleFont:14
                                         cornerRadius:0
                                      backgroundColor:BL_BlueBackGroundColor
                                      backgroundImage:nil
                                                image:nil];
    line.centerY = 22;
    [sectionHeader addSubview:line];
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(line.right + 20, 0, 200, 44)
                                                     text:title
                                                textColor:BL_firstTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    
    [sectionHeader addSubview:titleLable];
    return sectionHeader;
}
#pragma mark --Action Method
- (void)onSelectedControl:(STMenuControl*)control{
    NSLog(@"%@",control.titleLable.text);
    if (!self.userModel) {
        return;
    }
    NSString * name = control.titleLable.text;
    // 普通会员 限制
    if (self.userModel.card.integerValue == 3) {
        if ([name isEqualToString:@"布草增减"] ||
            [name isEqualToString:@"布草洗涤"] ||
            [name isEqualToString:@"查看库存"]) {
            //普通会员，不能进入 洗涤，增加和库存
            [SVProgressHUD showInfoWithStatus:@"现在您是普通会员，暂不能进行该操作哦"];
            return;
        }
        
    }
    
    if ([control.titleLable.text isEqualToString:@"布草增减"]) {
        [self.navigationController pushViewController:[BLClothMangeViewController new] animated:YES];
    }
    if ([control.titleLable.text isEqualToString:@"查看库存"]) {
        [self.navigationController pushViewController:[BLRepertoryViewController new] animated:YES];
        
    }
    if ([control.titleLable.text isEqualToString:@"洗涤充值"]) {
        [self.tabBarController setSelectedIndex:2];
    }
    if ([control.titleLable.text isEqualToString:@"消耗品"]) {
        //商城
        [self.navigationController pushViewController:[BLShopingViewController new] animated:YES];
    }
    if ([control.titleLable.text isEqualToString:@"布草定制"]) {
        [self.navigationController pushViewController:[BLClothCustomViewController new] animated:YES];
    }
    if ([control.titleLable.text isEqualToString:@"布草洗涤"]) {
         [self.navigationController pushViewController:[BLWathClosthHomeViewController new] animated:YES];
    }
}
- (void)sendMyInfoRequest{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php/Api/Basics/MemberListAction"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 self.userModel = [BLUserModel mj_objectWithKeyValues:responseObject[@"data"]];
                                                
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
@end
